package view;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import logic.Searcher;
import qw.easyFrame.enums.SelectionMode;
import qw.easyFrame.view.EasyFileChooser;
import qw.easyFrame.view.EasyTextArea;
import qw.easyFrame.view.listeners.RightClickListener;
import qw.easyFrame.view.util.SwingUtil;

/**
 * 主窗口.
 * @author Rex
 *
 */
public class MainFrame extends JFrame {
	private static final long serialVersionUID = 20211117L;
	
	/**
	 * 选中的文件.
	 */
	private static File[] chosen;
	
	/**
	 * 文件格式下拉框的首选项文字.
	 */
	public static String box_text = "选择文本编码 (默认UTF-8)";
	
	/**
	 * 待用户输入要搜索的文字的文本框内的默认文字.
	 */
	public static String field0_text = "输入要搜索的文字……";
	
	/**
	 * 待用户输入要搜索的文件格式的文本框内的默认文字.
	 */
	public static String field1_text = "输入要搜索的文件格式 (用空格分开, 默认txt html xml htm)";

	private MainFrame() {
		super("文本批量搜索工具");
		
		Font font = new Font("Microsoft yahei", Font.PLAIN, 12);
		
		JComboBox<String> box = new JComboBox<>();
		box.setFont(font);
		box.addItem(box_text);
		box.addItem("UTF-8");
		box.addItem("GBK");
		box.addItem("ISO-8859-1");
		box.addItem("GB2312");
		box.addItem("ASCII");
		box.addItem("UTF-16");
		box.addItem("GB18030");
		box.addItem("BIG5");
		box.addItem("UTF-32");
		box.addItem("GB12345-90");
		box.addItem("Shift_JIS");
		box.addItem("EUC-JP");
		box.addItem("ISO-2022-JP");
		
		JTextField field0 = SwingUtil.defaultJTextField(field0_text, font);
		JTextField field1 = SwingUtil.defaultJTextField(field1_text, font);
		
		JCheckBox check = new JCheckBox("忽略大小写");
		check.setFont(font);
		
        JButton btn0 = SwingUtil.defaultJButton("选择文件", font);
        JButton btn1 = SwingUtil.defaultJButton("开始搜索", font);
        
        EasyTextArea area = new EasyTextArea(font);
        JScrollPane scroll = new JScrollPane(area); 

		JPanel panel0_1 = SwingUtil.defaultJPanel(1, 2, 0, 0);
		panel0_1.add(btn0);
		panel0_1.add(btn1);
		JPanel panel0 = SwingUtil.defaultJPanel(1, 2, 0, 0);
		panel0.add(box);
		panel0.add(panel0_1);
		JSplitPane sp = new JSplitPane();
		sp.setDividerSize(0);
		sp.setDividerLocation(297);
		sp.setEnabled(false);
		sp.setLeftComponent(field0);
		sp.setRightComponent(check);
		JPanel panel2 = SwingUtil.defaultJPanel(1, 1, 0, 0);
		panel2.add(field1);
		JPanel panel3 = SwingUtil.defaultJPanel(3, 1, 0, 0);
		panel3.add(panel0);
		panel3.add(sp);
		panel3.add(panel2);
		
		Container cp = this.getContentPane();
        cp.add(panel3, BorderLayout.NORTH);
        cp.add(scroll, BorderLayout.CENTER);
        
        area.addMouseListener(new RightClickListener(area, font));
        EasyFileChooser chooser = new EasyFileChooser(area, SelectionMode.FILES_AND_DIRS);
		btn0.addActionListener(e -> chosen = chooser.choose());
		btn1.addActionListener(e -> Searcher.run(chosen, box, field0, field1, area, check));
		
		SwingUtil.setJFrame(this, 400, 600);
	}
	
	public static void main(String[] args) {
		new MainFrame();
	}

}